<?php

namespace app\api\controller;

use app\admin\model\patrol\Inspection;
use app\admin\model\shop\User;
use app\common\controller\Api;
use think\Config;
use think\Db;
use think\Exception;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];
    protected $failException = true;


    public function _initialize()
    {
        parent::_initialize();
        $this->modelUser = new \app\admin\model\userstaff\User();
        $this->modelShop = new User();//商户
        $this->modelInspection = new Inspection();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
    }


    /**
     * 执法公共部分
     * @return void
     */
    public function common()
    {

//       $user_info = $this->auth->getUserinfo();
        try {
            $array = [
                'inspection_num' => 0,  //街道数量
                'shop_num' => 0,//商户数量
                'cases_num' => 0, //案件数量
                'cases_green_num' => 0, //绿色
                'cases_yellow_num' => 0, //黄色
                'cases_red_num' => 0, //红色
            ];
            $array['inspection_num'] = $this->modelInspection->count('id');
            $array['shop_num'] = $this->modelShop->count('id');
            $cases = Db::table('fa_cases')
                ->field('
                    count(id) as cases_num,
                    sum(case when type=1 then 1 else 0 end) as cases_green_num,
                    sum(case when type=2 then 1 else 0 end) as cases_yellow_num,
                    sum(case when type=3 then 1 else 0 end) as cases_red_num
                ')->find();

            $this->success('ok', array_merge($array, $cases));
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 支队长案件进度
     * @return void
     */
    public function detachment_cases_plan()
    {
        $user_info = $this->auth->getUserinfo();
        try {
            $array = [
                'sign_num' => 0,  //待签收
                'being_processed_num' => 0,//处理中
                'verify_num' => 0, //核实
                'settle_num' => 0, //结案完成
            ];
            $cases = Db::table('fa_cases')
                ->where('detachment_id' , $user_info['staff_id'])
                ->field("
                    ifnull(sum(case when type=1 then 1 else 0 end),'0') as sign_num,
                    ifnull(sum(case when type=2 then 1 else 0 end),'0') as being_processed_num,
                    ifnull(sum(case when type=3 then 1 else 0 end),'0') as verify_num,
                    ifnull(sum(case when type=4 then 1 else 0 end),'0') as settle_num
                ")->find();
            $this->success('ok', array_merge($array, $cases));
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 支队长案件最多地区
     * @return void
     */
    public function detachment_cases_region()
    {
        $input = input();
        try {
            $cases = Db::table('fa_cases')->alias('m')
                ->join('user_staff_class c' , 'c.id=m.user_staff_class_id')
                ->field('count(user_staff_class_id) as num,c.name')
                ->group('user_staff_class_id')
                ->order('num desc')->cache('100')
                ->limit($input['num'])
                ->select();
            $this->success('ok', $cases);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }



    /**
     * 商户问题排行榜 月
     * @return void
     */
    public function detachment_shop_sort()
    {
        try {
            $cases = Db::table('fa_cases')->alias('m')
                ->join('cases_type c' , 'c.id=m.cases_type_id')
                ->whereTime('m.createtime','month')
                ->field('count(cases_type_id) as num,c.name')
                ->group('cases_type_id')
                ->order('num desc')
                ->select();
            $this->success('ok', $cases);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }



    /**
     * 队长案件进度
     * @return void
     */
    public function captain_cases_plan()
    {
        $user_info = $this->auth->getUserinfo();
        try {
            $array = [
                'sign_num' => 0,  //待签收
                'being_processed_num' => 0,//处理中
                'settle_num' => 0, //结案完成
            ];
            $cases = Db::table('fa_cases')
                ->where('detachment_id' , $user_info['staff_id'])
                ->field("
                    ifnull(sum(case when type=1 then 1 else 0 end),'0') as sign_num,
                    ifnull(sum(case when type=2 then 1 else 0 end),'0') as being_processed_num,
                    ifnull(sum(case when type=4 then 1 else 0 end),'0') as settle_num
                ")->find();
            $this->success('ok', array_merge($array, $cases));
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 巡检人员 日常巡查
     * @return void
     */
    public  function  inspection_task(){
        $user_info = $this->auth->getUserinfo();
        try {
            $array = [
                'num' => 0,#本周应巡查
                'stay_num' => 0, #待巡查
            ];
            $cases = Db::table('fa_patrol')
                ->where('user_staff_id' , $user_info['staff_id'])
                ->where('startstatus' , 2)
                ->whereTime('starttime','week')
                ->field("ifnull(sum(case when startstatus=2  then 1 else 0 end),'0') as num")
                ->field("ifnull(sum(case when startstatus=2 and status=1 then 1 else 0 end),'0') as stay_num")
                ->cache('10')
                ->find();
            $this->success('ok', array_merge($array, $cases));
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

}
